import away3d.core.base.Object3D;
import away3d.events.*;
import away3d.exporters.*;
import away3d.sprites.Sprite3D;

import com.powerfl.frm.taf.*;

import flash.display.*;
import flash.net.*;
import flash.utils.*;

final public function flashVar( $key:String ):String{
	return tdata( '@flashVar' )[$key] || '';
}
final public function urlOpen( $url:String, $isBlank:Boolean = true ):void{
	navigateToURL( new URLRequest( $url ), $isBlank ? '_blank' : '' );
}
public function javascript( $code:String ):void{
	orderAdd( null, _javascript, $code );
}
static private function _javascript( $code:String ):void{
	var key:*, arg:String;
	if( Security.sandboxType === Security.REMOTE ){
		if( ExternalInterface.available ){
			ExternalInterface.call( $code );	
		}
	}
}
//-----------------------------------------------------------------------------------------------------------
final public function renderView( ...$keys ):void{
	orderAdd( null, function():void{
		var key:*;
		for each( key in $keys ){
			tdata( key ).render();
		}
	} );
}
private function resizeView( $key:String ):void{
	var temp:View3D;
	temp = SELF.tdata( $key );
	temp.x = STAGE.stageWidth * .5; 
	temp.y = STAGE.stageHeight * .5;
	SELF._resize[SELF._resize.length] = function():void{
		temp = SELF.tdata( $key );
		temp.x = STAGE.stageWidth * .5; 
		temp.y = STAGE.stageHeight * .5;
	};
}
//------------------------------------------------------------------------------------------------------------------------------
static private function  setTransition():void{
	master.volume = masterInfo.volume * contextInfo.volume;
	master.pan = masterInfo.pan * contextInfo.pan;
	SoundMixer.soundTransform = master;
}
static public function volumeMain( $volume:Number, $pan:Number = 1 ):void{
	masterInfo.volume = $volume;
	masterInfo.pan = $pan;
	setTransition();
}
static public function volumeSub( $volume:Number, $pan:Number = 1 ):void{
	contextInfo.volume = $volume;
	contextInfo.pan = $pan;
	setTransition();
}
static public function soundPlay( $key:String, $loop:int = 1 ):void{
	SELF.tdataCall( $key, '', 'play', $loop );
}
static public function soundStop( ...$keys ):void{
	var i:int, j:int;
	for( i = 0 , j = $keys.length ; i < j ; ++i ){ 
		SELF.tdataCall( $keys[i], '', 'stop' );
	}
}
//------------------------------------------------------------------------------------------------------------------------------
final public function export( $key:String, $className:String, $pakageName:String = '' ):void{
	var export:AS3Exporter;
	export = new AS3Exporter;
	export.addEventListener( ExporterEvent.COMPLETE, function( $e:ExporterEvent ):void{
		$e.target.removeEventListener( $e.type, arguments.callee );
		trace( $e.data );
	} );
	export.export( tdata( $key ), $className, $pakageName );
}
private var screenPos:Object3D = new Object3D;
final public function getScreenPos( $viewKey:String, $key:String ):Array{
	var temp:*;
	temp = tdata( $key );
	if( temp is Sprite3D ){
		SELF.screenPos.x = temp.x + STAGE.stageWidth * .5;
		SELF.screenPos.y = temp.y;
		SELF.screenPos.z = temp.z;
		temp = tdata( $viewKey + '@camera' ).screen( SELF.screenPos );
	}else{
		temp = tdata( $viewKey + '@camera' ).screen( temp );
	}
	return [temp.x, -temp.y];
}
//------------------------------------------------------------------------------------------------------------------------------
static public function sharedGet( $group:String, $key:String ):*{
	var data:*;
	data = SharedObject.getLocal( $group ).data[$key];
	if( data === undefined ){
		return null;
	}else{
		return data;
	}
}
static public function sharedSet( $group:String, $key:String, $value:* ):void{
	SharedObject.getLocal( $group ).data[$key] = $value;
	SharedObject.getLocal( $group ).flush();
}
//------------------------------------------------------------------------------------------------------------------------------
static public function fullScreen( $area:Array = null, $fixMode:String = '' ):void{
	if( $fixMode){
		if( $fixMode === 'full' ){
			if( STAGE.displayState === StageDisplayState.NORMAL ){
				if( $area ){
					STAGE.fullScreenSourceRect = new Rectangle( $area[0], $area[1], $area[2], $area[3] );
				}else{
					STAGE.fullScreenSourceRect = new Rectangle( 0, 0, STAGE.stageWidth, STAGE.stageHeight );
				}
				STAGE.displayState = StageDisplayState.FULL_SCREEN;
			}
		}else{
			if( STAGE.displayState === StageDisplayState.FULL_SCREEN ){
				STAGE.displayState = StageDisplayState.NORMAL;
			}
		}
	}else{
		if( STAGE.displayState === StageDisplayState.NORMAL ){
			if( $area ){
				STAGE.fullScreenSourceRect = new Rectangle( $area[0], $area[1], $area[2], $area[3] );
			}else{
				STAGE.fullScreenSourceRect = new Rectangle( 0, 0, STAGE.stageWidth, STAGE.stageHeight );
			}
			STAGE.displayState = StageDisplayState.FULL_SCREEN;
		}else{
			STAGE.displayState = StageDisplayState.NORMAL;
		}
	}
}
//------------------------------------------------------------------------------------------------------------------------------
